package com.aaa.controller.back;

import com.aaa.entity.*;
import com.aaa.service.back.SalesService;
import com.aaa.service.back.WarehouseService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/sales")
@Controller
public class SalesController {

    @Resource
    private SalesService salesService;
    @Resource
    private WarehouseService warehouseService;

    /**
     * 客户订单页面显示
     * @return
     */
    @RequestMapping("/salesShow")
    public String SalesShow(Model model){
        List<Map<String, Object>> salesList1 = salesService.findSales1();
        model.addAttribute("salesList1",salesList1);

        List<Map<String, Object>> salesList2 = salesService.findSales2();
        model.addAttribute("salesList2",salesList2);

        List<Map<String, Object>> salesList3 = salesService.findSales3();
        model.addAttribute("salesList3",salesList3);
        return "back/sales-list";
    }

    /**
     * 客户订单明细页面显示
     * @return
     */
    @RequestMapping("/salesDetailsShow")
    public String salesDetailsShow(Model model,String salesId){
        List<Map<String, Object>> salesDetailsList = salesService.findSalesDetails(salesId);
        model.addAttribute("salesDetailsList",salesDetailsList);
        return "back/salesDetails-list";
    }

    /**
     * 更改客户订单待发货为待出库,并且生成客户出库单
     * @return
     */
    @RequestMapping("/editSales3")
    public String editSales3(Sales sales,String salesId){
        salesService.editSales3(sales);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String strDate = sdf.format(new Date());
        String salesOutId = "CK"+strDate;

        SalesOut salesOut = new SalesOut();
        salesOut.setSalesOutId(salesOutId);
        Emp emp = new Emp();
        emp.setEmpId(1);
        salesOut.setEmpId(emp.getEmpId());
        salesOut.setBusinessDate(new Date());
        salesOut.setSalesId(salesId);
        List<Map<String, Object>> salesList = salesService.findSales1();
        for (Map<String,Object> map : salesList){
            Integer userInfoId = (Integer) map.get("UserInfoId");
            salesOut.setUserInfoId(userInfoId);
        }

        salesService.addSalesOut(salesOut);

        List<Map<String, Object>> salesDetailsList = salesService.findSalesDetails(salesId);
        for (Map<String,Object> map : salesDetailsList){
            String goodsId = (String) map.get("goodsId");
            Integer goodsCount = (Integer) map.get("goodsCount");
            Integer totalAmount = (Integer) map.get("totalAmount");

            SalesOutDetails salesOutDetails = new SalesOutDetails();
            salesOutDetails.setSalesOutId(salesOutId);
            salesOutDetails.setGoodsId(goodsId);
            salesOutDetails.setGoodsCount(goodsCount);
            salesOutDetails.setGoodsPrice(totalAmount);
            salesService.addSalesOutDetails(salesOutDetails);
        }
        return "back/sales-list";
    }

    /**
     * 查询客户出库单
     * @return
     */
    @RequestMapping("/salesOutShow")
    public String salesOutShow(Model model){
        List<Map<String, Object>> salesOutList0 = salesService.findSalesOut0();
        model.addAttribute("salesOutList0",salesOutList0);

        List<Map<String, Object>> salesOutList1 = salesService.findSalesOut1();
        model.addAttribute("salesOutList1",salesOutList1);
        return "back/salesOut-list";
    }

    /**
     * 根据客户出库单编号查询客户出库单明细
     * @param model
     * @param salesOutId
     * @return
     */
    @RequestMapping("/salesOutDetailsShow")
    public String salesOutDetailsShow(Model model,String salesOutId){
        List<Map<String, Object>> salesOutDetailsList = salesService.findSalesOutDetails(salesOutId);
        model.addAttribute("salesOutDetailsList",salesOutDetailsList);
        return "back/salesOutDetails-list";
    }


    /**
     * 销售出库修改库存数量
     * @return
     */
    @RequestMapping("/editWarehouseDetails")
    public String editWarehouseDetails(Sales sales,SalesOut salesOut,String salesOutId){

        salesService.editSales4(sales);

        salesService.editSalesOut(salesOut);

        SalesOut salesOut1 = salesService.findBySalesOutId(salesOutId);

        List<SalesOutDetails> salesOutDetailsList = salesService.findBySalesOutDetails(salesOutId);

        for(SalesOutDetails salesOutDetails : salesOutDetailsList){
            salesService.editWarehouseDetails(salesOutDetails.getGoodsCount(),salesOutDetails.getGoodsId(),salesOut1.getWarehouseId());
        }

        return "back/salesOutDetails-list";
    }

}
